package app;

import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.view.View;
import android.widget.Button;

import com.example.menhoo.toollibrary.common.assist.Check;
import com.example.menhoo.toollibrary.common.assist.IMEHelper;
import com.example.menhoo.toollibrary.common.helper.MessageUtil;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.util.LogUtils;
import com.rey.material.widget.EditText;

import application.AppApplication;
import application.HttpUrlConstant;
import interfaces.IReturnObject;
import mainUI.AppActivity;
import menhoo.com.i8officelibrary.R;

/**
 * Created by yanglei on 15/11/1.
 */

public class ChangePassword extends AppActivity {

    AppCompatEditText editTextPwd;

    AppCompatEditText editTextNewPwd;

    AppCompatEditText editTextConfirmNewPwd;

    Button buttonSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //
        setContentView(R.layout.change_password);
        try{
            editTextPwd = (AppCompatEditText)findViewById(R.id.editTextPwd);
            editTextNewPwd = (AppCompatEditText)findViewById(R.id.editTextNewPwd);
            editTextConfirmNewPwd = (AppCompatEditText)findViewById(R.id.editTextConfirmNewPwd);
            buttonSubmit = (Button)findViewById(R.id.buttonSubmit);

            buttonSubmit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    editTextPwd.setError(null);
                    editTextNewPwd.setError(null);
                    editTextConfirmNewPwd.setError(null);

                    String editTextPwdStr = editTextPwd.getText().toString();
                    String editTextNewPwdStr = editTextNewPwd.getText().toString();
                    String editTextConfirmNewPwdStr = editTextConfirmNewPwd.getText().toString();

                    try{
                        if (Check.isEmpty(editTextPwdStr)) {
                            editTextPwd.setError("不能为空");
                        } else if (Check.isEmpty(editTextNewPwdStr)) {
                            editTextNewPwd.setError("不能为空");
                        }else if (Check.isEmpty(editTextConfirmNewPwdStr)) {
                            editTextConfirmNewPwd.setError("不能为空");
                        }else if (!editTextConfirmNewPwdStr.equalsIgnoreCase(editTextNewPwdStr)) {
                            editTextConfirmNewPwd.setError("密码不一致");
                        }else if(!editTextPwdStr.equalsIgnoreCase(AppApplication.getUser().pwd)){
                            MessageUtil.showShortToast(ChangePassword.this, "原始密码不正确");
                        }else{
                            RequestParams params = new RequestParams();
                            params.addQueryStringParameter("pwd",editTextConfirmNewPwdStr);
                            Post(HttpUrlConstant.getWebSiteFullUrl(HttpUrlConstant.ChangePassword), params, new IReturnObject<String>() {
                                @Override
                                public void returnResult(String result) {
                                    if (result != null) {
                                        onBackPressed();
                                        MessageUtil.showShortToast(ChangePassword.this, "密码修改成功");
                                    }
                                    else {
                                        MessageUtil.showShortToast(ChangePassword.this, "密码修改失败");
                                    }
                                }
                            });
                        }
                    }
                    catch (Exception e){
                        LogUtils.e(e.getMessage()+"");
                        MessageUtil.showShortToast(ChangePassword.this, "密码修改发生异常");
                    }
                }
            });
        }
        catch (Exception e){
            LogUtils.e(e.getMessage()+"");
        }

        setTitle("修改密码");
        setTitleDisplay(true);


    }
}
